package com.freshcity.page.wait
{
	import com.freshcity.component.FLabel;
	import com.freshcity.component.WaitIcon;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.HBox;
	import com.freshcity.component.container.VBox;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.OpCodes;
	import com.freshcity.core.SoundTypeConst;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.feiji.FeiJiViewWin;
	import com.freshcity.graphics.WaitTalkWin;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.page.feiji.FeiJiStatus;
	import com.freshcity.page.feiji.FeiJiView;
	import com.freshcity.page.feiji.FeiJiWriter;
	import com.freshcity.page.feiji.FeijiRank;
	import com.freshcity.page.search.QQUserList;
	import com.freshcity.page.search.RequestSelect;
	import com.freshcity.page.search.SelectTalker;
	import com.freshcity.page.search.ShowTalker;
	import com.freshcity.page.search.LatestUserList;
	import com.freshcity.page.talk.Talk;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.pblabs.debug.Logger;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import com.freshcity.page.help.UserQuestion;
	import com.freshcity.page.help.GainWealthDesc;

	public class WaitTalk extends Component
	{
		public var owner:RoomList;
		public var waitIcon:WaitIcon;
		public var back:WaitTalkWin;
		public var feijiWriter:FeiJiStatus;

		private var feijiView:FeiJiView;
		private var leftTime:int;
		private var gainDesc:GainWealthDesc;
		private var feijiRank:FeijiRank;
		private var _currentY:int;
		private var userList:LatestUserList;

		public function WaitTalk(xpos:Number=0, ypos:Number=0)
		{
			this.mouseEnabled=false;
			this.tabEnabled=false;
			super(null, xpos, ypos);
			back.closeBtn.addEventListener(MouseEvent.CLICK, onCancelWaitTalk);
		}

		protected function onCancelWaitTalk(event:MouseEvent):void
		{
			owner.group.commu.send(OpCodes.CANCEL_WAIT, {room: owner.queryRoom.id}, onCancel);
		}

		private function onCancel(data:*):void
		{
			owner.group.commu.unRegister(OpCodes.CANCEL_WAIT);
			owner.queryRoom=null;
			hide();
			//owner.win.refreshBtn.enabled=true;
			owner.win.tipMsg.text="选择不同主题场所，寻找新朋友。";
			TweenMax.to(this, 0.3, {autoAlpha: 0, onComplete: owner.onRefreshData});
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new WaitTalkWin();
			addChild(back);

			back.titleName.text="等待时刻,静候有缘人。";
			waitIcon=new WaitIcon(180, back.titleName.y - 5);
			//waitIcon.visible = false;
			addChild(waitIcon);

			//初始化link
			_currentY=50;
			createLink(">>我的纸飞机", onSendFeiJi);
			createLink(">>纸飞机世界", onReceiveFeiJi);
			createLink(">>发现有缘人", onLatestUsers);
			//createLink(">>QQ交友", onQQFriends);
			createLink(">>每周明星飞机", onFeiJiRank);
			createLink(">>交友指南", onIntroduceHere);
			createLink(">>意见与建议", onSuggestion);
		}

		private function createLink(label:String, callback:Function):void
		{
			var link:TextField=StringUtil.addHyLink(label, "", callback);
			link.x=120;
			link.y=_currentY;
			back.addChild(link);
			_currentY+=25;
		}

		override public function draw():void
		{
			super.draw();
			waitIcon.main=owner.group;

			feijiWriter=new FeiJiStatus();
			feijiWriter.name="feijiWrite";
			feijiWriter.main=owner.group;

			feijiView=new FeiJiView();
			feijiView.name="feijiView";
			feijiView.main=owner.group;

			feijiRank=new FeijiRank();
			feijiRank.name="feijiRank";
			feijiRank.main=owner.group;

			gainDesc=new GainWealthDesc();
			gainDesc.main=owner.group;

			//userList=new LatestUserList();
			//userList.main=this.owner.group;

			back.timeGift.toolTipData='保持在线，获取丰厚奖励';
			ToolTipsManager.register(back.timeGift);
			initTimeGift();

			addChild(feijiWriter);
			addChild(feijiView);
			addChild(feijiRank)
			addChild(gainDesc);
			addChild(userList);

		}

		private function onCallMyFriend(event:Event):void
		{
			ExternalInterface.call("shareFeed", "callMyFriend");
		}

		private function onIntroduceHere(event:Event):void
		{
			//var request:URLRequest=new URLRequest("http://www.thenicecity.com/help.html#01");
			//navigateToURL(request, "_blank");
			gainDesc.show();
		}

		private function onSuggestion(event:Event):void
		{
			var loc1:UserQuestion=new UserQuestion(this);
			loc1.main=owner.group;
		}


		private function onReceiveFeiJi(e:Event):void
		{
			feijiView.show();
		}

		private function onSendFeiJi(e:Event):void
		{
			if (owner.group.user.level < 3)
			{
				owner.group.msg.showRollTipsMsg("个人级别升到三级才可以投送飞机哦！");
			}
			else
			{
				feijiWriter.show();
			}
		}


		private function onLatestUsers(e:Event):void
		{
			userList.show();
		}

		private function onFeiJiRank(e:Event):void
		{
			owner.group.commu.send(OpCodes.READ_FEIJI_RANK, {}, rankCallback);
		}

		private function rankCallback(data:*):void
		{
			owner.group.commu.unRegister(OpCodes.READ_FEIJI_RANK);
			if (data is String)
			{
				owner.group.msg.showRollTipsMsg(data);
			}
			else
			{
				feijiRank.data=data;
				feijiRank.show();
			}
		}

		public function onShow(room:*):void
		{
			Logger.debug(this, "", "come in someroom...");
			owner.group.commu.register(OpCodes.BEGIN_WAIT, onComeIn);
			//owner.group.soundManager.playSound(SoundTypeConst.ComeIn, true);
			owner.group.commu.send(OpCodes.BEGIN_WAIT, {w: ""});

			waitIcon.room=room;
			waitIcon.isSynch=true;
			onTick();
		}

		private function onComeIn(data:*):void
		{
			var m:ComeInMsg=new ComeInMsg(owner.group);
			m.data=data;
		}

		private function onTick():void
		{
			if (!waitIcon.isSynch)
				return;

			this.leftTime--;
			if (leftTime >= 0)
			{
				back.timeGift.timePanel.text=StringUtil.formatTime(leftTime);
				TweenLite.delayedCall(1, onTick);
			}
			else
			{
				back.timeGift.gotoAndStop(2);
				back.timeGift.buttonMode=true;
				back.timeGift.addEventListener(MouseEvent.CLICK, onGetTimeReward);
			}
		}

		protected function onGetTimeReward(event:MouseEvent):void
		{
			back.timeGift.removeEventListener(MouseEvent.CLICK, onGetTimeReward);
			owner.group.commu.send(OpCodes.GET_TIME_REWARD, {}, getTimeRewardCallback);
		}

		private function getTimeRewardCallback(data:*):void
		{
			if (data == true)
			{
				owner.group.msg.showPopUpMsg("获得玫瑰三枚", MsgManager.COOL, 0);
				owner.group.msg.showFixRollMsg("在线等候累计达标，领取玫瑰三枚。");
			}
			else
			{
				owner.group.msg.showRollTipsMsg("抱歉，获取礼物失败。");
				Logger.error(this, "getTimeRewardCallback", String(data));
			}
			initTimeGift();
			TweenLite.delayedCall(1, onTick);
		}

		private function initTimeGift():void
		{
			back.timeGift.gotoAndStop(1);
			back.timeGift.buttonMode=false;
			this.leftTime=1800;
		}

		override public function hide(event:Event=null):void
		{
			owner.group.commu.unRegister(OpCodes.BEGIN_WAIT);
			waitIcon.isSynch=false;
		}
	}
}
